package com.copm.ifm.servers.sys.service.impl.dept;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.constant.NumberConstants;
import com.copm.ifm.base.basic.exception.ServiceException;
import com.copm.ifm.base.basic.msg.ExceptionMsg;
import com.copm.ifm.base.basic.msg.TreeMsg;
import com.copm.ifm.base.basic.pojo.vo.BaseTreeVO;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.base.service.tree.pageTree.PageTreeService;
import com.copm.ifm.servers.sys.entity.SysCategoryType;
import com.copm.ifm.servers.sys.entity.dept.SysDept;
import com.copm.ifm.servers.sys.mapper.dept.SysDeptMapper;
import com.copm.ifm.servers.sys.service.dept.SysDeptService;
import com.copm.ifm.servers.sys.vo.SysCategoryTypeVO;
import com.copm.ifm.servers.sys.vo.SysDeptVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 组织架构表 服务实现类
 *
 * @author tp
 * @since 2021-06-08
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService, PageTreeService<SysDept> {

    @Resource
    SysDeptMapper sysDeptMapper;

    @Override
    public BaseTreeVO getTree(int enabled, String name) {
        return getPageTree(enabled,name);
    }

    @Override
    public SysDept add(SysDept sysDept) {
        return pageAdd(sysDept);
    }

    @Override
    public SysDeptVO clickAddDept(Integer parentAreaId) {
        SysDeptVO sysDeptVO = new SysDeptVO();
        //首先默认为第一层级的分类
        int level = 1;
        SysDeptVO sysDept = new SysDeptVO();
        if (!StringUtils.isEmpty(parentAreaId)) {
            sysDept = sysDeptMapper.selectParentDeptById(parentAreaId);
            sysDeptVO.setCode(sysDept.getCode());
            sysDeptVO.setParentPath(sysDept.getPath());
            sysDeptVO.setName(sysDept.getName());
            level = sysDept.getLevel() + 1;
        }
        Integer maxSeq = sysDeptMapper.selectMaxSeq(sysDept.getPath(), level);
        sysDeptVO.setOrderNum(StringUtils.isEmpty(maxSeq) ? 10 : maxSeq / 10 * 10 + 10);
        sysDeptVO.setEnabled(DbConstants.DEFAULT_YES);
        return sysDeptVO;
    }

    @Override
    public SysDept modify(SysDept entity) {
        return pageModify(entity);
    }

    @Override
    public void updateDelete(Long id) {
        pageUpdateDelete(id);
    }

    @Override
    public SysDeptVO selectById(int id) {
        SysDeptVO sysDeptVO = sysDeptMapper.selectVoById(id);
        if (ObjectUtil.isEmpty(sysDeptVO)) {
            throw new ServiceException(ExceptionMsg.ENTITY_NOT_EXISTS);
        }
        sysDeptVO.setUpdateBy(AuthUtils.getCurrentUserId());
        sysDeptVO.setParentPathName(NumberConstants.NONE);
        String path = sysDeptVO.getPath();
        List<String> list = new ArrayList<>();
        for (int i = 1; i < sysDeptVO.getLevel(); i++) {
            list.add(path.substring(0, 4 * i));
        }
        list.add(path);
        List<SysDept> sysDepths = sysDeptMapper.selectList(new LambdaQueryWrapper<SysDept>()
                .in(SysDept::getPath, list)
                .eq(SysDept::getDeleted,DbConstants.DEFAULT_NO));
        List<String> codeList = sysDepths.stream().map(SysDept::getCode).collect(Collectors.toList());
        String fullPath = codeList.stream().map(String::valueOf).collect(Collectors.joining("-"));
        sysDeptVO.setFullCode(fullPath);
        if (!StringUtils.isEmpty(sysDeptVO)) {
            SysDept sysDeptParent = sysDeptMapper.selectOne(new LambdaQueryWrapper<SysDept>()
                    .eq(SysDept::getPath, sysDeptVO.getParentPath())
                    .eq(SysDept::getEnabled, DbConstants.DEFAULT_YES)
                    .eq(SysDept::getDeleted, DbConstants.DEFAULT_NO));
            if (!StringUtils.isEmpty(sysDeptParent)) {
                sysDeptVO.setParentPathName(sysDeptParent.getName());
                sysDeptVO.setParentId(sysDeptParent.getId());
            }
        }
        return sysDeptVO;
    }
}
